package com.yzt.rabbitmq.producer;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import com.yzt.rabbitmq.config.RabbitConfig;

//@Component
public class RabbitMQProducer {

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Scheduled(fixedDelay = 5000)
	public void send() {

		System.out.println("开始发送MQ消息");

		LocalDateTime date = LocalDateTime.now();
		String currentTime = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		String id = System.currentTimeMillis() + "";
		String msg = "当前时间：" + currentTime + ",发送内容为当前时间戳:" + id;

		Message message = MessageBuilder.withBody(msg.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON)
				.setContentEncoding("UTF-8").setMessageId(id).build();
		CorrelationData correlationData = new CorrelationData(id);
		rabbitTemplate.convertAndSend(RabbitConfig.DEFAULT_EXCHANGE, "", message, correlationData);
	}
}
